package com.uduemc.biso.master.backend.controller;

import java.io.IOException;
import java.util.List;

import javax.validation.Valid;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.validation.BindingResult;
import org.springframework.validation.FieldError;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.fasterxml.jackson.core.JsonParseException;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JsonMappingException;
import com.github.pagehelper.PageInfo;
import com.uduemc.biso.core.common.extities.CHostListCustomerData;
import com.uduemc.biso.core.extities.center.CustomerUser;
import com.uduemc.biso.core.extities.center.SysHostType;
import com.uduemc.biso.core.extities.center.SysLanguage;
import com.uduemc.biso.core.extities.center.SysServer;
import com.uduemc.biso.core.utils.JsonResult;
import com.uduemc.biso.master.backend.request.CustomermanageAppend;
import com.uduemc.biso.master.backend.service.CustomermanageService;
import com.uduemc.biso.master.backend.service.HostService;
import com.uduemc.biso.master.backend.service.ServermanageService;

@Controller
@RequestMapping("/backend/customermanage")
public class BCustomermanageController {

	@Autowired
	private HostService hostServiceImpl;

	@Autowired
	private ServermanageService servermanageServiceImpl;

	@Autowired
	private CustomermanageService customermanageServiceImpl;

	/**
	 * 添加用户数据渲染
	 * 
	 * @param model
	 * @return
	 * @throws JsonParseException
	 * @throws JsonMappingException
	 * @throws JsonProcessingException
	 * @throws IOException
	 */
	@GetMapping("/append")
	public ModelAndView append(ModelAndView model)
			throws JsonParseException, JsonMappingException, JsonProcessingException, IOException {
		List<SysHostType> listHostType = hostServiceImpl.getHostType();
		List<SysLanguage> listSysLanguage = hostServiceImpl.getSysLanguage();
		List<SysServer> listSysServer = servermanageServiceImpl.getServerByTypeStatus(0, (short) 1);

		model.addObject("listHostType", listHostType);
		model.addObject("listSysLanguage", listSysLanguage);
		model.addObject("listSysServer", listSysServer);
		model.setViewName("backend/customermanage/append");
		return model;
	}

	/**
	 * ajax 请求添加用户、站点数据
	 * 
	 * @param customermanageAppend
	 * @param error
	 * @return
	 * @throws JsonParseException
	 * @throws JsonMappingException
	 * @throws JsonProcessingException
	 * @throws IOException
	 */
	@PostMapping("/append/ajax")
	@ResponseBody
	public JsonResult appendAjax(@Valid @RequestBody CustomermanageAppend customermanageAppend, BindingResult error)
			throws JsonParseException, JsonMappingException, JsonProcessingException, IOException {
		if (error.hasErrors()) {
			for (FieldError fieldError : error.getFieldErrors()) {
				String defaultMessage = fieldError.getDefaultMessage();
				return JsonResult.messageWarning(defaultMessage);
			}
		}
		CustomerUser customerUser = customermanageServiceImpl.insertCustomerData(customermanageAppend);
		if (customerUser == null) {
			return JsonResult.assistance();
		}
		return JsonResult.messageSuccess("添加客户以及客户的主机信息成功！", customerUser);
	}

	/**
	 * 页面渲染
	 * 
	 * @param model
	 * @return
	 * @throws JsonParseException
	 * @throws JsonMappingException
	 * @throws JsonProcessingException
	 * @throws IOException
	 */
	@GetMapping("/infos")
	public ModelAndView infos(ModelAndView model)
			throws JsonParseException, JsonMappingException, JsonProcessingException, IOException {

		model.setViewName("backend/customermanage/infos");
		return model;
	}

	/**
	 * 通过条件参数过滤，获取用户数据信息列表
	 * 
	 * @param username
	 * @param numberKey
	 * @param status
	 * @param pageNumber
	 * @return
	 * @throws JsonParseException
	 * @throws JsonMappingException
	 * @throws JsonProcessingException
	 * @throws IOException
	 */
	@GetMapping("/infos/get")
	@ResponseBody
	public JsonResult infosGet(@RequestParam("username") String username, @RequestParam("numberKey") String numberKey,
			@RequestParam("status") short status,
			@RequestParam(value = "pageNumber", required = false, defaultValue = "1") int pageNumber)
			throws JsonParseException, JsonMappingException, JsonProcessingException, IOException {
		PageInfo<CHostListCustomerData> infosGet = customermanageServiceImpl.infosGet(0L, username, numberKey, status,
				pageNumber, 12);
		return JsonResult.ok(infosGet);
	}

	/**
	 * 通过参数id 获取单个用户总体信息数据 CHostListCustomerData
	 * 
	 * @param id
	 * @return
	 * @throws JsonParseException
	 * @throws JsonMappingException
	 * @throws JsonProcessingException
	 * @throws IOException
	 */
	@PostMapping("/infos/get-one-by-id")
	@ResponseBody
	public JsonResult infosGetOneById(@RequestParam("id") long id)
			throws JsonParseException, JsonMappingException, JsonProcessingException, IOException {
		CHostListCustomerData infoGet = customermanageServiceImpl.infoGet(id, 0L);
		return JsonResult.ok(infoGet);
	}

	/**
	 * 通过参数 username 获取用户信息，用于验证用户名是否被注册
	 * 
	 * @param username
	 * @return
	 * @throws JsonParseException
	 * @throws JsonMappingException
	 * @throws JsonProcessingException
	 * @throws IOException
	 */
	@PostMapping("/infos/get-one-by-username")
	@ResponseBody
	public JsonResult infosUpdateOneStatus(@RequestParam(value = "username", required = false) String username)
			throws JsonParseException, JsonMappingException, JsonProcessingException, IOException {
		if (StringUtils.isEmpty(username)) {
			return JsonResult.illegal();
		}
		CustomerUser byUsername = customermanageServiceImpl.getByUsername(username);
		return JsonResult.ok(byUsername);
	}

	/**
	 * 更新用户当前的状态
	 * 
	 * @param id
	 * @param status
	 * @return
	 * @throws JsonParseException
	 * @throws JsonMappingException
	 * @throws JsonProcessingException
	 * @throws IOException
	 */
	@PostMapping("/infos/update-one-status")
	@ResponseBody
	public JsonResult infosUpdateOneStatus(@RequestParam("id") long id, @RequestParam("status") short status)
			throws JsonParseException, JsonMappingException, JsonProcessingException, IOException {
		CHostListCustomerData infoGet = customermanageServiceImpl.infoGet(id, 0L);
		if (infoGet == null) {
			return JsonResult.illegal();
		}
		CustomerUser customerUser = customermanageServiceImpl.updateStatusCustomerUser(id, status);
		if (customerUser == null) {
			return JsonResult.assistance();
		}
		return JsonResult.messageSuccess("修改成功！", customerUser);
	}

	/**
	 * 修改当前用户的用户名
	 * 
	 * @param id
	 * @param username
	 * @return
	 * @throws JsonParseException
	 * @throws JsonMappingException
	 * @throws JsonProcessingException
	 * @throws IOException
	 */
	@PostMapping("/infos/update-one-username")
	@ResponseBody
	public JsonResult infosUpdateOneStatus(@RequestParam("id") long id, @RequestParam("username") String username)
			throws JsonParseException, JsonMappingException, JsonProcessingException, IOException {
		username = StringUtils.trimAllWhitespace(username);
		CustomerUser byUsername = customermanageServiceImpl.getByUsername(username);
		if (byUsername != null) {
			return JsonResult.messageWarning("用户名 " + username + " 已存在！");
		}
		CustomerUser customerUser = customermanageServiceImpl.updateUsernameCustomerUser(id, username);
		if (customerUser == null) {
			return JsonResult.assistance();
		}
		return JsonResult.messageSuccess("修改成功！", customerUser);
	}
}
